package com.project.webpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.webpage.aop.TagLog;
import com.project.webpage.dto.BaseParam;
import com.project.webpage.dto.GjxlParam;
import com.project.webpage.dto.GjxlRuleDto;
import com.project.webpage.service.GjxlService;
import com.project.webpage.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/webpage/gjxl/")
public class GjxlController extends BaseController {

    @Resource
    GjxlService gjxlService;

    /**
     * 选择列表
     */
    @PostMapping("list")
    public RestResult list(@RequestHeader(value = "user") String user) {
        return RestResult.ok(gjxlService.getList(user));
    }

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询线路列表")
    public RestResult page(@RequestBody GjxlParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(gjxlService.getListForPage(param)));
    }

    /**
     * 导出列表
     */
    @PostMapping("output")
    @TagLog(operation = 5, content = "导出线路列表")
    public RestResult output(@RequestBody GjxlParam param) {
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getListForPage(param));
    }

    /**
     * 未配置线路编码列表查询
     */
    @PostMapping("noxlbm/page")
    @TagLog(operation = 0, content = "查询未配置线路编码列表")
    public RestResult noXlbmPage(@RequestBody GjxlParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(gjxlService.getXlbmYuJingListForPage(param)));
    }

    /**
     * 未配置线路编码导出列表
     */
    @PostMapping("noxlbm/output")
    @TagLog(operation = 5, content = "导出未配置线路编码列表")
    public RestResult noXlbmOutput() {
        GjxlParam param = new GjxlParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getXlbmYuJingListForPage(param));
    }

    /**
     * 线路名称差异列表查询
     */
    @PostMapping("ncxlmc/page")
    @TagLog(operation = 0, content = "查询线路名称差异列表")
    public RestResult notcorrectXlmcPage() {
        BaseParam param = new BaseParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getNotCorrectXlmcListForPage(param));
    }

    /**
     * 首末站名称差异列表查询
     */
    @PostMapping("cysmzmc/page")
    @TagLog(operation = 0, content = "查询首末站名称差异列表")
    public RestResult CySmzmcPage() {
        BaseParam param = new BaseParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getCySmzmcListForPage(param));
    }

    /**
     * 线路站级差异列表查询
     */
    @PostMapping("cyzj/page")
    @TagLog(operation = 0, content = "查询线路站级差异列表")
    public RestResult cyZjPage() {
        BaseParam param = new BaseParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getCyZjListForPage(param));
    }

    /**
     * 计划配车数差异列表查询
     */
    @PostMapping("cypc/page")
    @TagLog(operation = 0, content = "查询计划配车数差异列表")
    public RestResult cyPcPage() {
        BaseParam param = new BaseParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getCyPcListForPage(param));
    }

    /**
     * 计划班次数差异列表查询
     */
    @PostMapping("cybc/page")
    @TagLog(operation = 0, content = "查询计划班次数差异列表")
    public RestResult cyBcPage() {
        BaseParam param = new BaseParam();
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getCyBcListForPage(param));
    }

    /**
     * 线路参数列表查询
     */
    @PostMapping("rule/page")
    @TagLog(operation = 0, content = "查询线路参数列表")
    public RestResult rulePage(@RequestBody GjxlParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(gjxlService.getRuleListForPage(param)));
    }

    /**
     * 导出线路参数列表
     */
    @PostMapping("rule/output")
    @TagLog(operation = 5, content = "导出线路参数列表")
    public RestResult ruleOutput(@RequestBody GjxlParam param) {
        this.getPage(false, param);
        return RestResult.ok(gjxlService.getRuleListForPage(param));
    }

    /**
     * 线路性质查询
     */
    @PostMapping("ruleid/list")
    public RestResult rule(@RequestBody GjxlRuleDto param) {
        return RestResult.ok(gjxlService.getRule(param));
    }

    /**
     * 更新线路性质
     */
    @PutMapping("rule/update")
    @TagLog(operation = 3, content = "更新线路性质")
    public RestResult ruleIdUpdate(@RequestBody GjxlRuleDto param) {
        GjxlParam gjxlParam = new GjxlParam();
        gjxlParam.setJgxlid(param.getJgxlid());
        List<GjxlRuleDto> list = gjxlService.getRuleListForPage(gjxlParam);
        if (list.size() == 0) {
            return RestResult.error("该条数据不存在");
        } else {
            try {
                gjxlService.updateRuleId(param);
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }

    /**
     * 更新线路编码
     */
    @PutMapping("xlbm/update")
    @TagLog(operation = 3, content = "更新线路编码")
    public RestResult xlbmUpdate(@RequestBody GjxlRuleDto param) {
        GjxlParam gjxlParam = new GjxlParam();
        gjxlParam.setJgxlid(param.getJgxlid());
        List<GjxlRuleDto> list = gjxlService.getRuleListForPage(gjxlParam);
        if (list.size() == 0) {
            return RestResult.error("该条数据不存在");
        } else {
            try {
                gjxlService.updateXlbm(param);
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }

    /**
     * 更新线路周末不考核
     */
    @PutMapping("noweekend/update")
    @TagLog(operation = 3, content = "更新线路周末不考核")
    public RestResult noWeekendUpdate(@RequestBody GjxlRuleDto param) {
        try {
            gjxlService.deleteNoweekend(param);
            if ("1".equals(param.getNoWeekend())) {
                gjxlService.insertNoweekend(param);
            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新线路单边考核
     */
    @PutMapping("single/update")
    @TagLog(operation = 3, content = "更新线路单边考核")
    public RestResult singleUpdate(@RequestBody GjxlRuleDto param) {
        try {
            gjxlService.deleteSingle(param);
            if (!"2".equals(param.getSingle())) {
                gjxlService.insertSingle(param);
            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新挂牌线路
     */
    @PutMapping("gp/update")
    @TagLog(operation = 3, content = "更新挂牌线路")
    public RestResult gpUpdate(@RequestBody GjxlRuleDto param) {
        try {
            gjxlService.deleteGp(param);
            if ("1".equals(param.getGp())) {
                gjxlService.insertGp(param);
            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新线路大间隔营运规范
     */
    @PutMapping("djg/update")
    @TagLog(operation = 3, content = "更新线路大间隔营运规范")
    public RestResult djgUpdate(@RequestBody GjxlRuleDto param) {
        try {
            GjxlParam gjxlParam = new GjxlParam();
            gjxlParam.setJgxlid(param.getJgxlid());
            GjxlRuleDto dto = gjxlService.getDjg(gjxlParam);
            if (dto == null) {
                gjxlService.insertDjg(param);
            } else {
                gjxlService.updateDjg(param);
            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }
}
